package com.neusoft.nep.PublicSupervisor.controller;

import com.neusoft.nep.entity.PublicSupervisorUser;
import com.neusoft.nep.common.AlertUtils;
import com.neusoft.nep.common.FileUtils;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class     RegisterController {
    @FXML
    private TextField txtPhoneNumber;
    @FXML
    private PasswordField txtPassword;
    @FXML
    private TextField txtUserName;
    @FXML
    private TextField txtUserBirthday;
    @FXML
    private PasswordField txtConfirmPassword;
    @FXML
    private ToggleGroup genderGroup;
    @FXML
    private Button btn_back;

    public void re() throws IOException {
        //获取Stage
        Stage stage = (Stage) btn_back.getScene().getWindow();
        //加载注册页面
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/Login.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
    public void saveUser() throws IOException {
        String phoneNumber = txtPhoneNumber.getText();
        String password = txtPassword.getText();
        String username = txtUserName.getText();
        String sex = ((RadioButton)genderGroup.getSelectedToggle()).getText();
        String userbirthday = txtUserBirthday.getText();
        String confirmpassword = txtConfirmPassword.getText();
        if (!password.equals(confirmpassword)) {
            System.out.println("两次输入密码不同");
            return;
        }
        if (phoneNumber.isEmpty() || userbirthday.isEmpty() || sex.isEmpty() || password.isEmpty()||username.isEmpty()) {
            AlertUtils.showDialog("注册失败", "请填写所有信息");
            return;
        }
        // 验证密码强度(至少六位）
        if (password.length() < 6) {
            AlertUtils.showDialog("注册失败", "密码至少需要6位");
            return;
        }
        PublicSupervisorUser u1 = new PublicSupervisorUser(phoneNumber,sex, userbirthday, password, username);
        Map<String,PublicSupervisorUser> userMap = FileUtils.readMapObject("PublicSupervisorUser.txt");
        userMap.put(u1.getPhoneNumber(),u1);
        FileUtils.writeMapObject("PublicSupervisorUser.txt",userMap);
        AlertUtils.showDialog("用户注册","注册成功");

    }

}
